<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增评分模板')" />
    <th:block th:include="include :: ztree-css" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-template-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">地区：</label>
                <div class="col-sm-8">

                    <select id="city" name="city" class="form-control m-b" th:with="type=${@dict.getType('b_city')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">模板名称：</label>
                <div class="col-sm-8">
                    <input name="templateName" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">模版引用：</label>
                <div class="col-sm-8">
                    <select id="templateId" name="templateId" class="form-control m-b">
                    </select>
                    <span class="help-block m-b-none" style="color:red"><i class="fa fa-info-circle"></i> 引用选择的模版评分类型及细则内容</span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">周期类型：</label>
                <div class="col-sm-8">
                    <select name="periodType" class="form-control m-b" th:with="type=${@dict.getType('b_period_type')}" required>
                        <option value="">所有</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">总分：</label>
                <div class="col-sm-8">
                    <input name="totalScore" class="form-control" type="text" required>
                </div>
            </div>

            <div class="form-group">    
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">
                    <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="status" checked>
                        <span></span>
                    </label>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea name="remark" class="form-control"></textarea>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: ztree-js" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script type="text/javascript">
	    $(function() {
			var url = ctx + "system/item/itemTreeData";
			var options = {
				id: "itemTrees",
		        url: url,
		        check: { enable: true },
		        expandLevel: 0
		    };
			$.tree.init(options);

            $.ajax({
                url: ctx + "system/template/getTemplate",
                type: 'GET',
                data: {
                    city: $("#city").select2('val')
                },
                success:function (data) {
                    console.log(data);
                    $("#templateId").empty();
                    $("#templateId").append("<option value=''>请选模板引用</option>");
                    for (var i = 0; i < data.data.length; i++){
                        $("#templateId").append("<option value=" + data.data[i].id + ">" + data.data[i].templateName + "</option>");
                    }
                }
            });
		});
        var prefix = ctx + "system/template"
        $("#form-template-add").validate({
            focusCleanup: true
        });
        $('input').on('ifChanged', function(obj){
			var type = $(this).val();
			var checked = obj.currentTarget.checked;
			if (type == 1) {
			    if (checked) {
			        $._tree.expandAll(true);
			    } else {
			        $._tree.expandAll(false);
			    }
			} else if (type == "2") {
			    if (checked) {
			        $._tree.checkAllNodes(true);
			    } else {
			        $._tree.checkAllNodes(false);
			    }
			} else if (type == "3") {
			    if (checked) {
			        $._tree.setting.check.chkboxType = { "Y": "ps", "N": "ps" };
			    } else {
			        $._tree.setting.check.chkboxType = { "Y": "", "N": "" };
			    }
			}
		})

        function submitHandler() {
            if ($.validate.form()) {
                add();
                //$.operate.save(prefix + "/add", $('#form-template-add').serialize()+"&abc="+abc);
            }
        }

        //模版引用列表
        $("#city").change(function () {//选择城市
            $.ajax({
                url: ctx + "system/template/getTemplate",
                type: 'GET',
                data: {
                    city: $("#city").select2('val')
                },
                success:function (data) {
                    console.log(data);
                    $("#templateId").empty();
                    $("#templateId").append("<option value=''>请选模板引用</option>");
                    for (var i = 0; i < data.data.length; i++){
                        $("#templateId").append("<option value=" + data.data[i].id + ">" + data.data[i].templateName + "</option>");
                    }
                }
            });
        });

        function add() {
			var city = $("select[name='city']").val();
			var periodType = $("select[name='periodType']").val();
			var templateName = $("input[name='templateName']").val();
			var totalScore = $("input[name='totalScore']").val();
			var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
			var remark = $("input[name='remark']").val();
            var templateId = $("select[name='templateId']").val();
			//var itemIds = $.tree.getCheckedNodes();
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/add",
				data : {
					"city": city,
					"templateName": templateName,
					"totalScore": totalScore,
					"status": status,
					"remark": remark,
					"periodType": periodType,
                    "templateId": templateId
				},
				async : false,
				error : function(request) {
					$.modal.alertError("系统错误");
				},
				success : function(data) {
					$.operate.successCallback(data);
				}
			});
		}
    </script>
</body>
</html>